//
//  JKTitleButton.swift
//  新浪微博
//
//  Created by Jack.Lin on 16/7/4.
//  Copyright © 2016年 Jack.Lin. All rights reserved.
//

import UIKit

class JKTitleButton: UIButton {

    init(title: String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: [])
        } else {
           setTitle(title! + " ", for: [])
          
           setImage(UIImage(named: "navigationbar_arrow_down"), for: [])
           setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)

        }
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray(), for: [])

        sizeToFit()

        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard  let titleLabel = titleLabel,
        imageView = imageView else {
            return
        }
        print("\(titleLabel) \(imageView)")
        
        
        
        // 将 label 的 x 向左移动 imageView 的宽度
        // swift 可以直接修改结构体里的值
        titleLabel.frame.origin.x = 0
        
        // 将 imageView 的 x 向右移动 label 的宽度
        imageView.frame.origin.x = titleLabel.bounds.width


        
    }
    
}
